package com.dh.cheesestrip.Util;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.anthonycr.grant.PermissionsManager;
import com.anthonycr.grant.PermissionsResultAction;
import com.dh.cheesestrip.Config;

import java.io.File;

/**
 * 各类杂七杂八方法
 *
 * @author M.c
 */
public class AndroidUtil {


    /**
     * 发短信
     *
     * @param phoneNumber 发送号码
     * @param smsBody     发送内容
     */
    public static void sendSms(String phoneNumber, String smsBody) {
        if (StringUtil.isBlank(phoneNumber)) {
            LogUtil.e("号码为空");
            return;
        }
        Uri uri = Uri.parse("smsto:" + phoneNumber);
        Intent it = new Intent(Intent.ACTION_SENDTO, uri);
        it.putExtra("sms_body", smsBody);
        Config.app().startActivity(it);
    }

    /**
     * 打电话
     *
     * @param phoneNumber 发送号码
     */
    public static void sendPhone(final Activity context, final String phoneNumber) {
        if (!StringUtil.isBlank(phoneNumber)) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(context);
            dialog.setTitle("提示").setMessage("确认拨打电话:" + phoneNumber + "?").setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @SuppressWarnings("MissingPermission")
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    final Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));

                    PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(context,
                            new String[]{Manifest.permission.CALL_PHONE}, new PermissionsResultAction() {

                                @Override
                                public void onGranted() {
                                    context.startActivity(intent);
                                }

                                @Override
                                public void onDenied(String permission) {
                                    AndroidUtil.toast("请允许拨号权限");
                                }
                            });

                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            });
            dialog.show();

        } else {
            LogUtil.e("号码为空");
        }
    }

    /**
     * 初始化webView控件
     *
     * @param webView 控件
     * @return 初始化完成的控件
     */
    public static WebView setWebView(WebView webView) {
        WebSettings set = webView.getSettings();
        webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setVerticalScrollBarEnabled(false);
        webView.requestFocus();
        set.setJavaScriptCanOpenWindowsAutomatically(true);
        set.setPluginState(WebSettings.PluginState.ON);
        set.setAllowFileAccess(true);
        set.setJavaScriptEnabled(true);
        set.setUseWideViewPort(true);
        set.setSupportZoom(false);
        set.setDomStorageEnabled(true);// 自动开启窗口
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return true;
            }
        });
        return webView;
    }

    private static Toast toast = null;

    /**
     * toast方法,零延时切换显示消息
     *
     * @param msg 要显示的消息
     */
    public static void toast(String msg) {
        if (null == msg || msg.equals("")) {
            return;
        }
        if (null == toast)
            toast = Toast.makeText(Config.app(), msg, Toast.LENGTH_SHORT);
        else
            toast.setText(msg);
        toast.show();
    }

    /**
     * activity跳转
     *
     * @param target 跳转Act名称
     */
    public static void intentAct(Context context, Class<?> target) {
        Intent intent = new Intent(Config.app(), target);
        context.startActivity(intent);
    }

    /**
     * 安装APK
     */
    public static void installApk(Context context, File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    /**
     * 打开本地pdf
     *
     * @param file
     */
    public static void openPdf(Context context, File file) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        context.startActivity(intent);
    }

    /**
     * 打开本地APP
     *
     * @param context
     * @param packageName 需打开APP的包名
     */
    public static void openAPP(Context context, String packageName) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        if (intent != null) {
            context.startActivity(intent);
        } else {
            toast("请安装APP");
        }
    }


}
